using System;
using System.Drawing;

namespace Camalon.Drawing
{
	/// <summary>
	/// Summary description for PolygonUtil.
	/// </summary>
	public class RectangleUtil
	{
		public static PointF CenterPoint( PointF[] polygonPoints)
		{
			return CenterPoint(GetBounds( polygonPoints ));
		}

		public static PointF CenterPoint( Point[] polygonPoints)
		{
			return CenterPoint(GetBounds( polygonPoints ));
		}

		public static PointF CenterPoint( RectangleF rectangle )
		{
			PointF center = rectangle.Location;//TopLeft
			center.Y = rectangle.Top + (rectangle.Height/2.0f);
			center.X = rectangle.Left + (rectangle.Width / 2.0f);
			return center;
		}

		public static PointF CenterPoint( Rectangle rectangle )
		{
			RectangleF b = rectangle;
			PointF center = b.Location;
			center.Y = b.Top + (b.Height/2.0f);
			center.X = b.Left + (b.Width / 2.0f);

			return center;
		}

		public static Rectangle GetBounds(Point[] polygonPoints)
		{
			//Find max x and max y value for bottom right corner
			//Find min x and min y value for topleft corner
			if( polygonPoints == null || polygonPoints.Length < 2) return Rectangle.Empty;
			int maxX	= int.MinValue;
			int maxY	= int.MinValue;
			int minX	= int.MaxValue;
			int minY	= int.MaxValue;

			foreach( Point pt in polygonPoints )
			{
				if( pt.X > maxX ) maxX = pt.X;
				if( pt.Y > maxY ) maxY = pt.Y;
				if( pt.X < minX ) minX = pt.X;
				if( pt.Y < minY ) minY = pt.Y;
			}

			return Rectangle.FromLTRB( minX, minY, maxX, maxY);
		}

		public static RectangleF GetBounds(PointF[] polygonPoints)
		{
			//Find max x and max y value for bottom right corner
			//Find min x and min y value for topleft corner
			if( polygonPoints == null || polygonPoints.Length < 2) return RectangleF.Empty;
			float maxX	= float.MinValue;
			float maxY	= float.MinValue;
			float minX	= float.MaxValue;
			float minY	= float.MaxValue;

			foreach( PointF pt in polygonPoints )
			{
				if( pt.X > maxX ) maxX = pt.X;
				if( pt.Y > maxY ) maxY = pt.Y;
				if( pt.X < minX ) minX = pt.X;
				if( pt.Y < minY ) minY = pt.Y;
			}

			return RectangleF.FromLTRB( minX, minY, maxX, maxY);
		}

		public static Point[] GetPoints( Rectangle bounds )
		{
			return new Point[]{ 
								  new Point( bounds.Left,  bounds.Top ),//topleft
								  new Point( bounds.Right ,bounds.Top ),//topright
								  new Point( bounds.Left , bounds.Bottom  ),//bottomLeft
								  new Point( bounds.Right, bounds.Bottom )};//bottomRight
		}

		public static PointF[] GetPoints( RectangleF bounds )
		{
			return new PointF[]{ 
								   new PointF( bounds.Left,  bounds.Top ),//topleft
								   new PointF( bounds.Right ,bounds.Top ),//topright
								   new PointF( bounds.Left , bounds.Bottom  ),//bottomLeft
								   new PointF( bounds.Right, bounds.Bottom )};//bottomRight
		}
	
	
	}
}
